package com.iuie.ns.frame.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtils extends org.apache.commons.io.FileUtils{
	
	public static void fileCopy(File sourceFile, File targetFile) {
		FileInputStream input = null;
		FileOutputStream output = null;
		BufferedInputStream reader = null;
		try {
			input = new FileInputStream(sourceFile);
			output = new FileOutputStream(targetFile);
			byte[] inputByte = new byte[1024*1024];
			reader = new BufferedInputStream(input, 1024*1024*2); // 从reader中取数据，加速数据传输
			int len = 0;
			while ((len = reader.read(inputByte)) != -1) {
				output.write(inputByte, 0, len);
			}
			output.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)
					reader.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			try {
				if (input != null)
					input.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			try {
				if (output != null)
					output.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}
	
	public static void fileCopy(InputStream sourceInputStream, File targetFile) {
		InputStream input = sourceInputStream;
		FileOutputStream output = null;
		BufferedInputStream reader = null;
		try {
			output = new FileOutputStream(targetFile);
			byte[] inputByte = new byte[1024*1024];
			reader = new BufferedInputStream(input, 1024*1024*2);
			int len = 0;
			while ((len = input.read(inputByte)) != -1) {
				output.write(inputByte, 0, len);
			}
			output.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			try {
				if (input != null)
					input.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			try {
				if (output != null)
					output.close();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}
	}

}
